package com.wenan.ui.controls.listBoxes
{
	import flash.events.Event;
	
	import mx.containers.Canvas;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;

	[Event(name="itemSelected", type="com.wenan.ui.controls.listBoxes.CheckListBoxItemEvent")]
	public class RadioListBox extends Canvas
	{
		private var dataGrid:DataGridSpecial;

		public function RadioListBox()
		{
			super();
			
			initilizeComponents();	
		}
		
		/**
		 * 数据接口。
		 */
		public function get dataProvider():Object
		{
			return dataGrid.dataProvider;
		}

		public function set dataProvider(value:Object):void
		{
			dataGrid.dataProvider = value;
		}

		/**
		 * 选择项。
		 */
		public function get selectItem():Object
		{
			return dataGrid.selectedItem;
		}
		
		public function set selectItem(value:Object):void
		{
			dataGrid.selectedItem = value;
		}
		
		/**
		 * 初始化控件。
		 */
		private function initilizeComponents():void
		{
			dataGrid = new DataGridSpecial();
			dataGrid.percentWidth = 100;
			dataGrid.percentHeight = 100;
			dataGrid.showHeaders = false;
			dataGrid.setStyle("verticalGridLineColor", "0xD0D4D7");
			dataGrid.setStyle("verticalScrollPolicy", "off");
			dataGrid.columns = buildDataGridColumns();
			dataGrid.addEventListener(CheckListBoxItemEvent.ITEM_SELECTED, itemSelectedHandler);
			addChild(dataGrid);
		}
		
		private function buildDataGridColumns():Array
		{
			var columns:Array = [];
			
			var checkColumn:CheckBoxColumn = new CheckBoxColumn();
			checkColumn.width = 25;
			checkColumn.sortable = false;
			checkColumn.headerRenderer = new ClassFactory(CheckBoxHeaderRender);
			checkColumn.itemRenderer = new ClassFactory(RadioButtomItemRenderer);
			columns.push(checkColumn);
			
			var nameColumn:DataGridColumn = new DataGridColumn();
			nameColumn.dataField = "name";
			columns.push(nameColumn);
			
			return columns;
		}
		
		private function itemSelectedHandler(event:Event):void 
		{
			var obj:Object = event;
			dispatchEvent(new CheckListBoxItemEvent(CheckListBoxItemEvent.ITEM_SELECTED, obj.item));
		}
	}
}